from __future__ import annotations

from decimal import Decimal
from typing import Optional

from pydantic import BaseModel, Field

from app.schemas.goods_sku import GoodsSkuBo


class GoodsInfoBase(BaseModel):
    goods_no: Optional[str] = Field(None, max_length=50, description="商品编号")
    goods_name: str = Field(..., max_length=200, description="商品名称")
    selling_point: str = Field("", max_length=1000, description="商品卖点")
    spec_type: int = Field(..., ge=10, le=20, description="规格类型：10 单规格 20 多规格")
    goods_price_min: Decimal = Field(..., decimal_places=2, ge=0, description="最低售价")
    goods_price_max: Decimal = Field(..., decimal_places=2, ge=0, description="最高售价")
    line_price_min: Decimal = Field(..., decimal_places=2, ge=0, description="最低划线价")
    line_price_max: Decimal = Field(..., decimal_places=2, ge=0, description="最高划线价")
    content: Optional[str] = Field(None, description="商品介绍（富文本）")
    delivery_id: int = Field(..., gt=0, description="配送方式ID")
    is_points_gift: int = Field(0, ge=0, le=1, description="是否积分赠品 1=是 0=否")
    catalog_id: Optional[int] = Field(None, ge=0, description="所属分类id")
    weight: Optional[Decimal] = Field(None, decimal_places=4, ge=0, description="重量(kg)")
    status: Optional[int] = Field(None, ge=0, le=1, description="上架状态 0-下架 1-上架")
    goods_sales: int = Field(0, ge=0, description="商品销量")
    goods_image_id: str = Field("", max_length=255, description="图片ids，逗号分隔")
    brand_id: Optional[int] = Field(None, ge=0, description="品牌id")
    after_sales_service: str = Field("", max_length=1000, description="售后服务")

    @classmethod
    def _to_decimal(cls, v):
        return v if v is None else Decimal(str(v))

    @classmethod
    def _strip_str(cls, v):
        return v.strip() if isinstance(v, str) else v


class GoodsInfoBo(GoodsInfoBase):
    # sku信息
    goodsSkuList: list[GoodsSkuBo]
    # 销售属性信息
    pass


class GoodsInfoVo(GoodsInfoBase):
    goods_id: int = Field(..., gt=0, description="商品主键")

    class Config:
        from_attributes = True

class SpuAttributeValueBo(GoodsInfoBo):
    id: int
    spu_id: int
    attr_id: int
    attr_name: str
    attr_value: Optional[str]
    attr_sort:int